/*---------------------------------------------------------
* FILE: StreamAttachmentDataSource.java
* LOGICAL NAME: StreamAttachmentDataSource
* PRODUCT: bila-framework
*----------------------------------------------------------
*/

package bila.framework.mail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.core.io.AbstractResource;

/**
 * @author WJuarez
 * <b>Created by </b>WJuarez
 * <b>for </b>bila-framework
 * <b>on </b>01/02/2011 16:04:34
 * <b>Purpose</b> 
 * <p>
 * 		Preparacion de archivo a anexar en los correos
 * </p>
 */
public class StreamAttachmentDataSource extends AbstractResource{

    private ByteArrayOutputStream outputStream;
    private String name;
    private String contentType;

    public StreamAttachmentDataSource(InputStream inputStream, String name, String contentType) {
        this.outputStream = new ByteArrayOutputStream();
        this.name = name;
        this.contentType = contentType;

        int read;
        byte[] buffer = new byte[256];

        try {
            while ((read = inputStream.read(buffer)) != -1)
                getOutputStream().write(buffer, 0, read);
        } catch (IOException e) {

        }
    }

    public String getDescription() {
        return "Stream resource used for attachments";
    }

    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(this.outputStream.toByteArray());
    }

    public String getContentType() {
        return contentType;
    }

    public String getName() {
        return name;
    }

    public ByteArrayOutputStream getOutputStream() {
        return outputStream;
    }
}
